using System;
using System.Collections.Generic;
using System.Windows.Threading;
namespace Microsoft.Phone.Controls
{
	public static class HubTileService
	{
		private const int WaitingPipelineSteps = 3;
		private const int NumberOfSimultaneousAnimations = 1;
		private const bool TrackResurrection = false;
		private static DispatcherTimer Timer;
		private static Random ProbabilisticBehaviorSelector;
		private static List<WeakReference> EnabledImagesPool;
		private static List<WeakReference> FrozenImagesPool;
		private static List<WeakReference> StalledImagesPipeline;
		static HubTileService()
		{
			HubTileService.Timer = new DispatcherTimer();
			HubTileService.ProbabilisticBehaviorSelector = new Random();
			HubTileService.EnabledImagesPool = new List<WeakReference>();
			HubTileService.FrozenImagesPool = new List<WeakReference>();
			HubTileService.StalledImagesPipeline = new List<WeakReference>();
			HubTileService.Timer.add_Tick(new EventHandler(HubTileService.OnTimerTick));
		}
		private static void RestartTimer()
		{
			if (!HubTileService.Timer.get_IsEnabled())
			{
				HubTileService.Timer.set_Interval(TimeSpan.FromMilliseconds(2500.0));
				HubTileService.Timer.Start();
			}
		}
		internal static void InitializeReference(HubTile tile)
		{
			WeakReference tile2 = new WeakReference(tile, false);
			if (tile.IsFrozen)
			{
				HubTileService.AddReferenceToFrozenPool(tile2);
			}
			else
			{
				HubTileService.AddReferenceToEnabledPool(tile2);
			}
			HubTileService.RestartTimer();
		}
		internal static void FinalizeReference(HubTile tile)
		{
			WeakReference tile2 = new WeakReference(tile, false);
			HubTileService.RemoveReferenceFromEnabledPool(tile2);
			HubTileService.RemoveReferenceFromFrozenPool(tile2);
			HubTileService.RemoveReferenceFromStalledPipeline(tile2);
		}
		private static void AddReferenceToEnabledPool(WeakReference tile)
		{
			if (!HubTileService.ContainsTarget(HubTileService.EnabledImagesPool, tile.get_Target()))
			{
				HubTileService.EnabledImagesPool.Add(tile);
			}
		}
		private static void AddReferenceToFrozenPool(WeakReference tile)
		{
			if (!HubTileService.ContainsTarget(HubTileService.FrozenImagesPool, tile.get_Target()))
			{
				HubTileService.FrozenImagesPool.Add(tile);
			}
		}
		private static void AddReferenceToStalledPipeline(WeakReference tile)
		{
			if (!HubTileService.ContainsTarget(HubTileService.StalledImagesPipeline, tile.get_Target()))
			{
				HubTileService.StalledImagesPipeline.Add(tile);
			}
		}
		private static void RemoveReferenceFromEnabledPool(WeakReference tile)
		{
			HubTileService.RemoveTarget(HubTileService.EnabledImagesPool, tile.get_Target());
		}
		private static void RemoveReferenceFromFrozenPool(WeakReference tile)
		{
			HubTileService.RemoveTarget(HubTileService.FrozenImagesPool, tile.get_Target());
		}
		private static void RemoveReferenceFromStalledPipeline(WeakReference tile)
		{
			HubTileService.RemoveTarget(HubTileService.StalledImagesPipeline, tile.get_Target());
		}
		private static bool ContainsTarget(List<WeakReference> list, object target)
		{
			for (int i = 0; i < list.get_Count(); i++)
			{
				if (list.get_Item(i).get_Target() == target)
				{
					return true;
				}
			}
			return false;
		}
		private static void RemoveTarget(List<WeakReference> list, object target)
		{
			for (int i = 0; i < list.get_Count(); i++)
			{
				if (list.get_Item(i).get_Target() == target)
				{
					list.RemoveAt(i);
					return;
				}
			}
		}
		private static void OnTimerTick(object sender, EventArgs e)
		{
			HubTileService.Timer.Stop();
			for (int i = 0; i < HubTileService.StalledImagesPipeline.get_Count(); i++)
			{
				if ((HubTileService.StalledImagesPipeline.get_Item(i).get_Target() as HubTile)._stallingCounter-- == 0)
				{
					HubTileService.AddReferenceToEnabledPool(HubTileService.StalledImagesPipeline.get_Item(i));
					HubTileService.RemoveReferenceFromStalledPipeline(HubTileService.StalledImagesPipeline.get_Item(i));
					i--;
				}
			}
			if (HubTileService.EnabledImagesPool.get_Count() > 0)
			{
				for (int j = 0; j < 1; j++)
				{
					int num = HubTileService.ProbabilisticBehaviorSelector.Next(HubTileService.EnabledImagesPool.get_Count());
					switch ((HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile).State)
					{
					case ImageState.Expanded:
						if ((HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile)._canDrop || (HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile)._canFlip)
						{
							if (!(HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile)._canDrop && (HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile)._canFlip)
							{
								(HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile).State = ImageState.Flipped;
							}
							else
							{
								if (!(HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile)._canFlip && (HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile)._canDrop)
								{
									(HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile).State = ImageState.Semiexpanded;
								}
								else
								{
									if (HubTileService.ProbabilisticBehaviorSelector.Next(2) == 0)
									{
										(HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile).State = ImageState.Semiexpanded;
									}
									else
									{
										(HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile).State = ImageState.Flipped;
									}
								}
							}
						}
						break;
					case ImageState.Semiexpanded:
						(HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile).State = ImageState.Collapsed;
						break;
					case ImageState.Collapsed:
						(HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile).State = ImageState.Expanded;
						break;
					case ImageState.Flipped:
						(HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile).State = ImageState.Expanded;
						break;
					}
					(HubTileService.EnabledImagesPool.get_Item(num).get_Target() as HubTile)._stallingCounter = 3;
					HubTileService.AddReferenceToStalledPipeline(HubTileService.EnabledImagesPool.get_Item(num));
					HubTileService.RemoveReferenceFromEnabledPool(HubTileService.EnabledImagesPool.get_Item(num));
				}
			}
			else
			{
				if (HubTileService.StalledImagesPipeline.get_Count() == 0)
				{
					return;
				}
			}
			HubTileService.Timer.set_Interval(TimeSpan.FromMilliseconds((double)(HubTileService.ProbabilisticBehaviorSelector.Next(1, 31) * 100)));
			HubTileService.Timer.Start();
		}
		public static void FreezeHubTile(HubTile tile)
		{
			WeakReference tile2 = new WeakReference(tile, false);
			HubTileService.AddReferenceToFrozenPool(tile2);
			HubTileService.RemoveReferenceFromEnabledPool(tile2);
			HubTileService.RemoveReferenceFromStalledPipeline(tile2);
		}
		public static void UnfreezeHubTile(HubTile tile)
		{
			WeakReference tile2 = new WeakReference(tile, false);
			HubTileService.AddReferenceToEnabledPool(tile2);
			HubTileService.RemoveReferenceFromFrozenPool(tile2);
			HubTileService.RemoveReferenceFromStalledPipeline(tile2);
			HubTileService.RestartTimer();
		}
		public static void FreezeGroup(string group)
		{
			for (int i = 0; i < HubTileService.EnabledImagesPool.get_Count(); i++)
			{
				if ((HubTileService.EnabledImagesPool.get_Item(i).get_Target() as HubTile).GroupTag == group)
				{
					(HubTileService.EnabledImagesPool.get_Item(i).get_Target() as HubTile).IsFrozen = true;
					i--;
				}
			}
			for (int j = 0; j < HubTileService.StalledImagesPipeline.get_Count(); j++)
			{
				if ((HubTileService.StalledImagesPipeline.get_Item(j).get_Target() as HubTile).GroupTag == group)
				{
					(HubTileService.StalledImagesPipeline.get_Item(j).get_Target() as HubTile).IsFrozen = true;
					j--;
				}
			}
		}
		public static void UnfreezeGroup(string group)
		{
			for (int i = 0; i < HubTileService.FrozenImagesPool.get_Count(); i++)
			{
				if ((HubTileService.FrozenImagesPool.get_Item(i).get_Target() as HubTile).GroupTag == group)
				{
					(HubTileService.FrozenImagesPool.get_Item(i).get_Target() as HubTile).IsFrozen = false;
					i--;
				}
			}
			HubTileService.RestartTimer();
		}
	}
}
